package com.qiyu.admin.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qiyu.admin.system.model.Department;
import com.qiyu.admin.system.service.DepService;
import com.qiyu.admin.system.service.UserService;
import com.qiyu.admin.system.util.ModelUtil;
import com.qiyu.framework.util.QiYuUtil;
import com.qiyu.framework.util.ResponseJson;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * 部门控制器
 *
 * @author zhangqing
 * @date 2017年04月01日
 */
@RestController
@RequestMapping(value = "dep")
public class DepController {
    @Resource
    private DepService depService;
    @Resource
    private UserService userService;

    @RequestMapping(value = "/update/view",method = RequestMethod.GET)
    public ModelAndView depUpdatView(String depId,ModelMap modelMap) throws Exception{
        modelMap.addAttribute("depInfo",depService.selectByPrimaryKey(depId));
        ModelAndView modelAndView = new ModelAndView("system/dep/dep_update",modelMap);
        return modelAndView;

    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResponseJson addDep(Department department) throws Exception{
        ModelUtil.insertInit(department);
        Department resultDep = depService.insertSelective(department);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",resultDep.getId());
        jsonObject.put("pId",resultDep.getParentId());
        jsonObject.put("name",resultDep.getDepName());
        return QiYuUtil.getResponseJson("部门添加成功",true,jsonObject);
    }
    @RequestMapping(value = "",method = RequestMethod.DELETE)
    public ResponseJson deleteDep(Department department) throws Exception{
        department.setActiveFlag(-1);//状态改为-1,代表逻辑删除
        ModelUtil.updateInit(department);
        depService.updateByPrimaryKeySelective(department);//修改操作
        return QiYuUtil.getResponseJson("删除成功",true,null);
    }
    @RequestMapping(value = "",method = RequestMethod.PUT)
    public ResponseJson updateDep(Department department) throws Exception{
        ModelUtil.updateInit(department);
        Department resultDep = depService.updateByPrimaryKeySelective(department);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",resultDep.getId());
        jsonObject.put("pId",resultDep.getParentId());
        jsonObject.put("name",resultDep.getDepName());
        return QiYuUtil.getResponseJson("部门修改成功",true,jsonObject);
    }

    @RequestMapping(value = "info/{id}")
    public ModelAndView depInfo(@PathVariable(value = "id")String id,ModelMap modelMap) throws Exception{
        modelMap.addAttribute("depInfo",depService.selectByPrimaryKey(id));
        ModelAndView modelAndView = new ModelAndView("system/dep/dep_info",modelMap);
        return modelAndView;
    }

    @RequestMapping(value = "/main",method = RequestMethod.GET)
    public ModelAndView depMain(){
        return new ModelAndView("system/dep/dep_main");
    }

    @RequestMapping(value = "/add/view",method = RequestMethod.GET)
    public ModelAndView addView(String depId, ModelMap modelMap){
        modelMap.addAttribute("parentId",depId);
        return new ModelAndView("system/dep/dep_add");
    }

    @RequestMapping(value = "/json/list",method = RequestMethod.GET)
    public ResponseJson depJsonData() throws Exception{
        return QiYuUtil.getResponseJson("查询成功",true,JSONArray.toJSON(depService.getDepZtreeData()));
    }
}
